
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 04/20/2011 12:42:52
-- Generated from EDMX file: D:\@yuxinyang\v4\YiDeSoft\BigTrunk\EzDesk-Dev\Services\Apps\EzDesk.Apps.Data\Message\EzDeskMessage.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [EzDeskMessage];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_MessageMessageInbox]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[MessageInboxSet] DROP CONSTRAINT [FK_MessageMessageInbox];
GO
IF OBJECT_ID(N'[dbo].[FK_TopicSingleMessage]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SingleMessageSet] DROP CONSTRAINT [FK_TopicSingleMessage];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[MessageInboxSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[MessageInboxSet];
GO
IF OBJECT_ID(N'[dbo].[SingleMessageSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[SingleMessageSet];
GO
IF OBJECT_ID(N'[dbo].[TopicSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[TopicSet];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'MessageInboxSet'
CREATE TABLE [dbo].[MessageInboxSet] (
    [UserId] uniqueidentifier  NOT NULL,
    [TopicId] uniqueidentifier  NOT NULL,
    [ConfirmOn] datetime  NULL,
    [Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'SingleMessageSet'
CREATE TABLE [dbo].[SingleMessageSet] (
    [MessageId] uniqueidentifier  NOT NULL,
    [MessageTitle] nvarchar(200)  NULL,
    [MessageText] nvarchar(max)  NOT NULL,
    [SenderId] uniqueidentifier  NOT NULL,
    [CreateOn] datetime  NOT NULL,
    [ParentMessageId] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'TopicSet'
CREATE TABLE [dbo].[TopicSet] (
    [MessageId] uniqueidentifier  NOT NULL,
    [MessageTitle] nvarchar(200)  NULL,
    [MessageText] nvarchar(max)  NOT NULL,
    [Memberstring] nvarchar(1000)  NOT NULL,
    [SenderId] uniqueidentifier  NOT NULL,
    [CreateOn] datetime  NOT NULL,
    [NeedConfirm] bit  NOT NULL,
    [ReplyCount] int  NOT NULL,
    [ConfirmCount] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'MessageInboxSet'
ALTER TABLE [dbo].[MessageInboxSet]
ADD CONSTRAINT [PK_MessageInboxSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [MessageId] in table 'SingleMessageSet'
ALTER TABLE [dbo].[SingleMessageSet]
ADD CONSTRAINT [PK_SingleMessageSet]
    PRIMARY KEY CLUSTERED ([MessageId] ASC);
GO

-- Creating primary key on [MessageId] in table 'TopicSet'
ALTER TABLE [dbo].[TopicSet]
ADD CONSTRAINT [PK_TopicSet]
    PRIMARY KEY CLUSTERED ([MessageId] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [TopicId] in table 'MessageInboxSet'
ALTER TABLE [dbo].[MessageInboxSet]
ADD CONSTRAINT [FK_MessageMessageInbox]
    FOREIGN KEY ([TopicId])
    REFERENCES [dbo].[TopicSet]
        ([MessageId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_MessageMessageInbox'
CREATE INDEX [IX_FK_MessageMessageInbox]
ON [dbo].[MessageInboxSet]
    ([TopicId]);
GO

-- Creating foreign key on [ParentMessageId] in table 'SingleMessageSet'
ALTER TABLE [dbo].[SingleMessageSet]
ADD CONSTRAINT [FK_TopicSingleMessage]
    FOREIGN KEY ([ParentMessageId])
    REFERENCES [dbo].[TopicSet]
        ([MessageId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_TopicSingleMessage'
CREATE INDEX [IX_FK_TopicSingleMessage]
ON [dbo].[SingleMessageSet]
    ([ParentMessageId]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------